package com.twentyOne.ClothingLand.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.twentyOne.ClothingLand.service.IStorageService;
import com.twentyOne.ClothingLand.service.impl.StorageService;
import com.twentyOne.ClothingLand.util.JsonUtil;
import com.twentyOne.ClothingLand.vo.Storage;

public class StorageAction extends ActionSupport {
	
	private Storage storage;
	private IStorageService storageService = new StorageService();
	
	public Storage getStorage() {
		return storage;
	}

	public void setStorage(Storage storage) {
		this.storage = storage;
	}

	public String addStorage() throws Exception {

		storageService.addStorage(storage);
		return null;

	}
	
	public String searchAllStorage() throws Exception {
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		List<Storage> list = storageService.searchAllStorage();
		
		if (list != null) {
			String storagesJson = JsonUtil.objectsToJson(list);
			out.print(storagesJson);
		} else {
			out.print("[{\"Nothing\":\"Nothing\"}]");
		}
		return null;

	}

}
